import { ConfigEnv, UserConfigExport } from 'vite'
import vue from '@vitejs/plugin-vue'
import { viteMockServe } from 'vite-plugin-mock'
import { resolve } from 'path'
import { loadEnv } from 'vite';

export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  // 获取根路径
  const root = process.cwd();
  // 获取环境变量文件的内容
  const envInfo = loadEnv(mode, root);
  return {
    plugins: [
      vue(),
      viteMockServe({
        // default
        mockPath: 'mock',
        localEnabled: command === 'serve',
      }),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, "src") //设置别名
      }
    },
    server: {
      host: 'localhost',
      port: Number(envInfo.VITE_APP_PORT),
      open: true,
      proxy: {
        '/api': {
          target: 'http://api.vikingship.xyz/api',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
  }
}
